package com.homework.ch02;

import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/10/23
 * @Description:
 */
public class Test07 {
    public static void main(String[] args) {
        //将流转换为其他数据类型
        //1.stream变成数组
        Stream<Integer> s1= Stream.of(10,20,30,40);
        //Integer[] arr1= (Integer[]) s1.toArray();
        Object[] arr1 = s1.toArray();
        System.out.println(Arrays.toString(arr1));
        Stream<Object> s2=Stream.of("hello",12,'a');
        Object[] arr2 = s2.toArray();
        System.out.println(Arrays.toString(arr2));
        //System.out.println(s2.count());//报错，因为toArray也是一种终端操作
        Stream<String> s3=Stream.of("hello","world");
        String[] arr3=s3.toArray(String[]::new);
        Integer[] arr4=Stream.of(1,2,3,4,5).toArray(Integer[]::new);//只能是类类型
        LongStream s4=LongStream.range(1,10);
        long[] arr6=s4.toArray();//可以获得基本数据类型
        System.out.println(Arrays.toString(arr6));
        //stream到集合
        Stream<String> s5=Stream.of("abc","hello","world");
        List<String> list1=s5.collect(Collectors.toList());
        System.out.println(list1.getClass().getName());
        Stream<String> s6=Stream.of("abc","hello","world");
        List<String> list2=s6.collect(Collectors.toCollection(LinkedList::new));
        System.out.println(list2.getClass().getName());
        Stream<String> s7=Stream.of("abc","hello","world","abc","hello");
        Set<String> set1=s7.collect(Collectors.toSet());//默认转化为hashSet
        System.out.println(set1.getClass().getName());
        Stream<String> s8=Stream.of("abc","hello","world","abc","hello");
        Set<String> set2=s8.collect(Collectors.toCollection(TreeSet::new));
        System.out.println(set2);
        System.out.println(set2.getClass().getName());
        Stream<String> s9=Stream.of("abc","hello","world");//不允许重复
        Map<String,Integer> map=s9.collect(Collectors.toMap(Function.identity(),s->s.length()));
        System.out.println(map);
        Stream<String> s10=Stream.of("abc","hello","world","abc","hello");
        String result1=s10.collect(Collectors.joining("@"));//按照连接符连接
        System.out.println(result1);
    }
}
